/*
 * Copyright (c) 2010 - 2011, Brookhaven National Laboratory
 * All rights reserved.
 * The software in this package is published under the terms
 * of the BSD license a copy of which has been included with
 * this distribution in the license.txt file.
 */


package org.oxtypes;

import org.omg.dds.type.TypeKind;
import org.omg.dds.type.dynamic.DynamicType;
// import org.omg.dds.type.dynamic.DynamicData;

import org.oxtypes.impl.PrimitiveImpl.*;
import org.oxtypes.impl.SequenceImpl.*;
import org.oxtypes.impl.StructureImpl;

/**
 *
 */
public class DynamicDataFactory 
        extends org.omg.dds.type.dynamic.DynamicDataFactory {

    /** Returns singleton */
    static public DynamicDataFactory getInstance(){
        if(s_instance == null){
            s_instance = new DynamicDataFactory();
        }
        return s_instance;
    }

    public DynamicData createData() {
        throw new java.lang.UnsupportedOperationException();
    }

    public DynamicData createData(DynamicType type){

        switch(type.getKind()){
            case NO_TYPE:
            case BOOLEAN_TYPE:
            case BYTE_TYPE:
            case INT_16_TYPE:
            case UINT_16_TYPE:
            case INT_32_TYPE:
            case UINT_32_TYPE:
            case INT_64_TYPE:
            case UINT_64_TYPE:
                throw new java.lang.UnsupportedOperationException();
            case FLOAT_32_TYPE: 
            case FLOAT_64_TYPE: return new DoubleData();
            case FLOAT_128_TYPE:
            case CHAR_8_TYPE:
            case CHAR_32_TYPE:

            case ENUMERATION_TYPE:
            case BITSET_TYPE:
            case ALIAS_TYPE:

            case ARRAY_TYPE:
                throw new java.lang.UnsupportedOperationException();
            case SEQUENCE_TYPE: return createSequence(type);
            case STRING_TYPE:
            case MAP_TYPE:

            case UNION_TYPE:
                throw new java.lang.UnsupportedOperationException();
            case STRUCTURE_TYPE: return new StructureImpl(type);
            case ANNOTATION_TYPE:
            default:
                throw new java.lang.UnsupportedOperationException();
        }
        
    }

    //

    private DynamicData createSequence(DynamicType type){

        DynamicType et = type.getDescriptor().getElementType();
        int bound = type.getDescriptor().getBound().get(0);

        switch(et.getKind()){
            case NO_TYPE:
            case BOOLEAN_TYPE:
            case BYTE_TYPE:
            case INT_16_TYPE:
            case UINT_16_TYPE:
            case INT_32_TYPE:
            case UINT_32_TYPE:
            case INT_64_TYPE:
            case UINT_64_TYPE:
                throw new java.lang.UnsupportedOperationException();
            case FLOAT_32_TYPE:
            case FLOAT_64_TYPE: return new DoubleSequence(bound);
            case FLOAT_128_TYPE:
            case CHAR_8_TYPE:
            case CHAR_32_TYPE:

            case ENUMERATION_TYPE:
            case BITSET_TYPE:
            case ALIAS_TYPE:

            case ARRAY_TYPE:
            case SEQUENCE_TYPE:
            case STRING_TYPE:
            case MAP_TYPE:

            case UNION_TYPE:
            case STRUCTURE_TYPE:
            case ANNOTATION_TYPE:
            default:
                throw new java.lang.UnsupportedOperationException();
        }
    }

    private DynamicDataFactory(){
    }

    private static DynamicDataFactory s_instance;

}
